$(function(){
	ShowData();
});
function WithdraSel(){
	ShowData();
}
function ShowData(){
	//获取用户姓名
	var user = document.getElementById("userid").value;
	//获取审批状态
	var statu = $('#statype option:selected').val();
	//alert(user);
	user=encodeURI(encodeURI(user));
	var url = "withdrawal/all.do?usaccount="+user+"&status="+statu;
	
	var tableSource = new $.jqx.dataAdapter({
		dataType: "json",
		dataFields:[
		    { name: "id",type:"int"},
		    { name: "usaccount",type:"string"},
		    { name: "username", type: "string" },
		    { name: "bank_name", type: "string" },
		    { name: "bank_city", type: "string" },
		    { name: "bank_account_no", type: "string" },
		    { name: "bank_account_name", type: "string" },
		    { name: "withdraw_type",type:"string"},
		    { name: "withdraw_account",type:"string"},
		    { name: "money",type:"double"},
		    { name: "rate",type:"double"},
		    { name: "isok",type:"float"},
		    { name: "status",type:"int"},
		    { name: "account",type:"string"},
		    { name: "created_at",type:"date"},
		    { name: "updated_at",type:"date"}
		],
		id: "id",
		url:url
	});
	ShowView(tableSource);
}
function ShowView(tableSource){
	$("#dataTable").jqxDataTable({
		width: "100%",
        source: tableSource,
        pageable: true,
        pageSize: 15,
        serverProcessing: true,
        pagerButtonsCount: 10,
        columnsResize: true,
        sortable: true,
        columns: [
            { text: "序号", width: 50, cellsRenderer: function(row) { return (row + 1).toString(); } },
            { text: "用户账号",dataField:"usaccount"},
            { text: "用户名称",dataField:"username"},
            { text:	"银行名称",dataField:"bank_name", cellsRenderer: function(row, column, value, rowData) {
//            	alert(rowData.bank_name);
            	var bankName = rowData.bank_name;
            	$.each(bank, function(index, bankInfo){
            		if(rowData.bank_name == bankInfo['code']){
            			bankName = bankInfo['name'];
            			return false;
            		}
            	});
            	return bankName;
            }},
            { text: "开户省州",dataField:"bank_city"},
            { text: "银行账号",dataField:"bank_account_no"},
            { text: "银行用户名",dataField:"bank_account_name"},
            { text: "交易金额",dataField:"money"},
            { text: "手续费",dataField:"rate"},
            { text: "余额是否充足",dataField:"isok",
            	cellsRenderer:function(row, column, value, rowData){
            		if(rowData.isok>=0){
            			return "余额充足";
            		}else{
            			return "余额不足";
            		}
            	}
            },
            { text: "提现类型",dataField:"withdraw_type"},
            { text: "审批人",dataField:"account"},
            { text: "申请时间",dataField:"created_at",
            	cellsRenderer: function(row, column, value, rowData) {
            		return format(value, "Y-M-D H:I");
            	}
            },
            { text: "审批",dataField:"status",width:200,
            	cellsRenderer:function(row, column, value, rowData){
            			return ["<input class='btn btn-sm btn-primary' type='button' value=' 通过 ' onclick='approve(this, " + rowData.id + ","+rowData.isok+", 1,"+rowData.money+",\""  + rowData.bank_account_no +"\",\"" + rowData.bank_account_name + "\")'/> , " +
            			       
                            	"<input class='btn btn-sm btn-danger' type='button' value='不通过' onclick='approve(this, " + rowData.id + ","+rowData.isok+", 3,0)'/>",
                            	"<b style='color:#008B00'>已通过</b>","<b style='color:#BB0000'>未通过</b>","<b style='color:#BB0000'>未通过</b>"][value];
            	}
            },
            { text:"审批时间",dataField:"updated_at",
            	cellsRenderer: function(row, column, value, rowData) {
        		return format(value, "Y-M-D H:I");
        		}
            }
        ],
	});
}
function approve(me, sellerId,isok,status,money,bank_account_no,bank_account_name){
	if(isok<0&&status==1){
		layer.confirm("余额不足！该项审核不能通过",{
			icon:2,
			title:"操作提示",
			closeBtn:false,
			btn : [ "确定", "取消" ]
		});
	}else{
		_index = layer.confirm("确实审核它吗？该操作不能撤销。", {
			icon : 3,
			title : "操作提示",
			closeBtn : false,
			skin : "layui-layer-red",
			btn : [ "确定", "取消" ]
		}, 
		function() {
			me = me.parentNode;
			$.ajax({
				url : "withdrawal/updateById.do",
				type:"post",
				data : {
					id : sellerId,
					status : status,
					money : money,
					bank_account_no: bank_account_no,
					bank_account_name: bank_account_name
				},
				async : true,
				success : function(result) {
					me.innerHTML = [ "待审核","已通过","未通过","已取消"][status];
				}
			});
			me.innerHTML = "<span style='color:#888;'>正在提交..</span>";
			layer.close(_index);
		});
	}
}